package com.mida.app.knw.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.mida.app.knw.service.DetailCodingSystemService;
import com.mida.app.knw.service.KnwSlotService;
import com.mida.dms.knw.api.KnwSlotApi;
import com.mida.dms.knw.entity.KnwConceptualDomain;
import com.mida.dms.knw.entity.KnwSlot;
import com.mida.dms.knw.entity.vo.KnwSlotVo;
import com.mida.dms.knw.entity.vo.MapperPage;
import com.mida.dms.knw.vo.KnwSlotPageVo;
import com.mida.dms.knw.vo.PageResultVo;
import com.mida.tool.common.result.ActionResult;
import com.mida.tool.common.result.ListResult;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <h1>本体slot服务实现</h1>
 * <p>
 *
 * </p>
 *
 * @author jian.li on 2021/6/22 10:44
 */
@Service
@Slf4j
public class KnwSlotServiceImpl implements KnwSlotService {

    @Autowired
    DetailCodingSystemService detailCodingSystemService;

    @Autowired
    KnwSlotApi knwSlotApi;

    @Override
    public ActionResult updateDetailCoding(Long slotSoid, Long detailCodingSystemSoid) {
        return knwSlotApi.updateDetailCoding(slotSoid, detailCodingSystemSoid);
    }

    @Override
    public ActionResult publish(Long noumenonSoid, List<KnwSlot> list) {
        return knwSlotApi.publish(noumenonSoid, list);
    }

    @Override
    public ActionResult add(KnwSlotVo knwSlot) {
        return knwSlotApi.add(knwSlot);
    }

    @Override
    public ListResult<PageResultVo<KnwSlotVo>> find(KnwSlotPageVo vo) {

        ListResult<PageResultVo<KnwSlotVo>> page = knwSlotApi.findPage(vo);
        if (ObjectUtils.isNotEmpty(page) && HttpStatus.OK.value() == page.getStatus()) {
            PageResultVo<KnwSlotVo> data = page.getData();
            if (ObjectUtils.isNotEmpty(data)) {
                List<KnwSlotVo> list = data.getList();
                if (CollectionUtils.isNotEmpty(list)) {
                    // 值集
                    List<Long> collect = list.stream().filter(f -> ObjectUtils.isNotEmpty(f.getDataType())).map(e -> e.getDataType()).collect(Collectors.toList());
                    if (CollectionUtils.isNotEmpty(collect)) {
                        Map<Long, String> dataSetNameMap = detailCodingSystemService.getDataSetNameMap(collect);
                        if (MapUtils.isNotEmpty(dataSetNameMap)) {
                            list.forEach(k -> k.setDataSetMeaning(dataSetNameMap.get(k.getDataType())));
                        }
                    }

                    // 编码体系
                    List<Long> detailCollect = list.stream().filter(f -> ObjectUtils.isNotEmpty(f.getDerailCodingSystemSoid())).map(e -> e.getDerailCodingSystemSoid()).collect(Collectors.toList());
                    if(CollectionUtils.isNotEmpty(detailCollect)){
                        Map<Long, String> detailCodingNameMap = detailCodingSystemService.getDetailCodingNameMap(detailCollect);
                        if(MapUtils.isNotEmpty(detailCodingNameMap)){
                            list.forEach(k -> k.setDerailCodingSystemName(detailCodingNameMap.get(k.getDerailCodingSystemSoid())));
                        }
                    }

                    List<Long> referenceQualitativeMaintenanceCollect = list.stream().filter(f -> ObjectUtils.isNotEmpty(f.getReferenceQualitativeMaintenance())).map(e -> e.getReferenceQualitativeMaintenance()).collect(Collectors.toList());
                    if(CollectionUtils.isNotEmpty(referenceQualitativeMaintenanceCollect)){
                        Map<Long, String> detailCodingNameMap = detailCodingSystemService.getDetailCodingNameMap(referenceQualitativeMaintenanceCollect);
                        if(MapUtils.isNotEmpty(detailCodingNameMap)){
                            list.forEach(k -> k.setReferenceQualitativeMaintenanceName(detailCodingNameMap.get(k.getReferenceQualitativeMaintenance())));
                        }
                    }

                }
            }
        }

        return page;
    }

//    public ListResult<KnwSlotVo> find(KnwSlotPageVo vo) {
////        return knwSlotApi.findPage(vo);
//        return null;
//    }

    @Override
    public ListResult<KnwSlotVo> findBySoid(Long soid) {

        ListResult<KnwSlotVo> result = knwSlotApi.findBySlotSoid(soid);
        if (ObjectUtils.isNotEmpty(result) && HttpStatus.OK.value() == result.getStatus()) {
            KnwSlotVo data = result.getData();
            if (ObjectUtils.isNotEmpty(data)) {
                // 值集
                if (ObjectUtils.isNotEmpty(data.getDataType())) {
                    String dataSetName = detailCodingSystemService.getDataSetName(data.getDataType());
                    data.setDataSetMeaning(dataSetName);
                }

                // 编码体系
                if (ObjectUtils.isNotEmpty(data.getDerailCodingSystemSoid())) {
                    String detailCodingName = detailCodingSystemService.getDetailCodingName(data.getDerailCodingSystemSoid());
                    data.setDerailCodingSystemName(detailCodingName);
                }

                // 参考值定性维护名称
                if (ObjectUtils.isNotEmpty(data.getReferenceQualitativeMaintenance())) {
                    String detailCodingName = detailCodingSystemService.getDetailCodingName(data.getReferenceQualitativeMaintenance());
                    data.setReferenceQualitativeMaintenanceName(detailCodingName);
                }

            }
        }
        return result;
    }

    @Override
    public ActionResult delete(KnwSlotVo vo) {
        return knwSlotApi.delete(vo);
    }

    @Override
    public ActionResult order(Long originalSlotOrder, Long newSlotOrder, Long slotSoid) {
        return knwSlotApi.order(originalSlotOrder, newSlotOrder, slotSoid);
    }

    @Override
    public ListResult<Object> update(KnwSlotVo vo) {
        return knwSlotApi.update(vo);
    }

    @Override
    public ListResult<Object> selectSlotNameByCode(MapperPage vo) {
        return knwSlotApi.selectSlotNameByCode(vo);
    }

    @Override
    public ListResult<Object> getDataBySlotName(KnwSlotVo vo) {
        return knwSlotApi.getDataBySlotName(vo);
    }

    @Override
    public ListResult<Object> getDataByDomainId(KnwConceptualDomain domain) {
        return knwSlotApi.getDataByDomainId(domain);
    }

    @Override
    public ListResult<List<KnwSlotVo>> findAllSoltBynoumenonSoid(Long noumenonSoid) {

        ListResult<List<KnwSlotVo>> result = knwSlotApi.findAllSoltBynoumenonSoid(noumenonSoid);
        if (ObjectUtils.isNotEmpty(result) && HttpStatus.OK.value() == result.getStatus()) {
            List<KnwSlotVo> data = result.getData();
            if (CollectionUtils.isNotEmpty(data)) {
                // 值集
                List<Long> collect = data.stream().map(e -> e.getUnit()).collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(collect)) {
                    Map<Long, String> dataSetNameMap = detailCodingSystemService.getDataSetNameMap(collect);
                    Map<Long, String> dataSetDescriptionMap = detailCodingSystemService.getDataSetDescriptionMap(collect);
                    if (MapUtils.isNotEmpty(dataSetNameMap)) {
                        data.forEach(k -> k.setUnitName(dataSetNameMap.get(k.getUnit())));
                    }

                    if (MapUtils.isNotEmpty(dataSetDescriptionMap)) {
                        data.forEach(d -> d.setUnitDescription(dataSetDescriptionMap.get(d.getUnit())));
                    }
                }

                // 编码体系
                List<Long> detailCollect = data.stream().filter(f -> ObjectUtils.isNotEmpty(f.getDerailCodingSystemSoid())).map(e -> e.getDerailCodingSystemSoid()).collect(Collectors.toList());
                if(CollectionUtils.isNotEmpty(detailCollect)){
                    Map<Long, String> detailCodingNameMap = detailCodingSystemService.getDetailCodingNameMap(detailCollect);
                    if(MapUtils.isNotEmpty(detailCodingNameMap)){
                        data.forEach(k -> k.setDerailCodingSystemName(detailCodingNameMap.get(k.getDerailCodingSystemSoid())));
                    }
                }

                List<Long> referenceQualitativeMaintenanceCollect = data.stream().filter(f -> ObjectUtils.isNotEmpty(f.getReferenceQualitativeMaintenance())).map(e -> e.getReferenceQualitativeMaintenance()).collect(Collectors.toList());
                if(CollectionUtils.isNotEmpty(referenceQualitativeMaintenanceCollect)){
                    Map<Long, String> detailCodingNameMap = detailCodingSystemService.getDetailCodingNameMap(referenceQualitativeMaintenanceCollect);
                    if(MapUtils.isNotEmpty(detailCodingNameMap)){
                        data.forEach(k -> k.setReferenceQualitativeMaintenanceName(detailCodingNameMap.get(k.getReferenceQualitativeMaintenance())));
                    }
                }
            }
        }
        return result;
    }
}
